import java.io.*;
import java.util.*;

public class Main {
	String input;
	StringTokenizer st;
	BufferedReader bf;
	StringBuilder sb;
	
	public Main() {
		bf = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
	}
	
	public static void main(String[] args) {
		Main main = new Main();
		main.beginProcess();
	}

	void beginProcess() {
		int n;
		try {
			while((input = bf.readLine()) != null) {
				st = new StringTokenizer(input);
				n = Integer.parseInt(st.nextToken());
				while(n > 0) {
					getPostfix(bf.readLine());
					n--;
				}
			}
			System.out.print(sb);
		}
		catch(Exception ex) {
			System.err.println(ex.toString());
		}
	}
	
	void getPostfix(String infixStr) {
		int i;
		char curChar;
		String curOperandStr,
		       postfixStr;
		Stack<Character> stack;
		curOperandStr = "";
		postfixStr = "";
		stack = new Stack<Character>();
		for(i = 0; i < infixStr.length(); i++) {
			curChar = infixStr.charAt(i);
			if(curChar == ' ') continue;
			if(isOperator(curChar)) {
				if(!curOperandStr.equals("")) {
					postfixStr += curOperandStr + " ";
					curOperandStr = "";
				}
				if(curChar == '(') {
					stack.push(curChar);
				} else if(curChar == ')') {
					while(!(stack.peek() == '('))
						postfixStr += stack.pop() + " ";
					stack.pop();
				} else {
					while((!stack.isEmpty()) &&
					      priorityOfOperatorInStack(stack.peek()) >= priorityOfOperatorInStack(curChar))
						postfixStr += stack.pop() + " ";
					stack.push(curChar);
				}
			} else {
				if(isDigit(curChar)) curOperandStr += curChar;
				else postfixStr += curChar + " ";
			}
		}
		if(!curOperandStr.equals("")) {
			postfixStr += curOperandStr + " ";
			curOperandStr = "";
		}
		while(!stack.isEmpty())
			postfixStr += stack.pop() + " ";
		postfixStr = postfixStr.substring(0, postfixStr.length() - 1);
		sb.append(postfixStr + "\n");
	}
	
	boolean isOperator(char curChar) {
		if(curChar == '(' || curChar == ')' ||
		   curChar == '^' ||
		   curChar == '*' || curChar == '/' ||
		   curChar == '+' || curChar == '-')
			return true;
		return false;
	}
	
	boolean isDigit(char curChar) {
		if(curChar >= '0' && curChar <= '9') return true;
		return false;
	}
	
	int priorityOfOperatorInStack(char operator) {
		if(operator == '(' || operator == ')') return 0;
		else if(operator == '^') return 3;
		else if(operator == '*' || operator == '/') return 2;
		else return 1;
	}
	
}